#pragma once
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <iostream>
#include <cstdio>
using namespace std;
using namespace cv;

enum MY_SORT_TYPE
{
	MP_XSYS = 0,
	MP_XSYL = 1,
	MP_XLYS = 2,
	MP_XLYL = 3,
	MP_YSXS = 4,
	MP_YLXS = 5,
	MP_YSXL = 6,
	MP_YLXL = 7,
	MP_XY_SUMS = 8
};
class RRPoint
{
public:
	Point2f p0, p1, p2, p3;
	RRPoint(RotatedRect& f, int st)
	{
		Point2f input_points[4]; f.points(input_points);
		int bg = 0;
		if (st <= 7)
		{
			for (int i = 0; i < 4; i++)
				if (mycmp(input_points[i], input_points[bg], 
					(st >> 2) & 1, (st >> 1) & 1, st & 1))
					bg = i;
		}
		else
		{
			for (int i = 0; i < 4; i++)
				if ((input_points[i].x + input_points[i].y)
					< (input_points[bg].x + input_points[bg].y - eps))
					bg = i;
		}
		
		p0 = input_points[bg];
		p1 = input_points[(bg + 1) % 4];
		p2 = input_points[(bg + 2) % 4];
		p3 = input_points[(bg + 3) % 4];
	}


	const static double eps;
	static bool mycmp(Point2f a, Point2f b, 
		bool XoY/*0 for x first,1 for y*/, 
		bool XSoL/*0 for x small,1 for large*/,
		bool YSoL/*0 for y small,1 for large*/)
	{
		float ca[2] = { a.x,a.y }, cb[2] = { b.x,b.y };
		if (XoY == 1) swap(ca[0], ca[1]), swap(cb[0], cb[1]), swap(XSoL, YSoL);
		if (fabs(ca[0] - cb[0]) < eps)
			return (YSoL == 0) ? ca[1]<cb[1] - eps : ca[1]>cb[1] + eps;
		else
			return (XSoL == 0) ? ca[0] < cb[0] - eps : ca[0]>cb[0] + eps;
	}
};
const double RRPoint::eps = 1e-7;